//
//  XMStatusoriginalView.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
// cell间距
let cellMargin: CGFloat = 5
// cell宽高
let cellWH: CGFloat = 80
class XMStatusOriginalView: UIView {
    
    // 记录当前原创微博的底部约束
    var orginalViewBottomConstraint: Constraint?
    
    // 定义一个viewModel供外界调用
    var statusViewModel: XMStatusViewModel?{
        didSet{
            // 设置头像
            headImageView.xm_setImage(statusViewModel?.status?.user?.profile_image_url, placeolderImgName: "avatar_default")
            // 昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            // 时间和来源
            timeLabel.text = statusViewModel?.sinaTimeStr
//            timeLabel.attributedText = statusViewModel.
            
//            sourceLabel.attributedText = getSourceStr(statusViewModel?.status?.soure)
            // MVC
            // sourceLabel.attributedText = statusViewModel?.status?.sourceAttr
            // MVVM
            sourceLabel.attributedText = statusViewModel?.sourceAttr
            // 微博会员等级
            nemberImageView.image = statusViewModel?.memberImage
            // 微博认证用户
            avaterImageView.image = statusViewModel?.avatarImage
            
            // 文本内容
            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalContentAttr
            // 配图
            // 卸载约束
            orginalViewBottomConstraint?.uninstall()
            
            // 代表有配图
            if statusViewModel?.status?.pic_urls?.count > 0{
                // 赋值
                pictureView.pictureInfo = statusViewModel?.status?.pic_urls
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
                })
                // 显示配图
                pictureView.hidden = false
            }else{
                // 代表没有配图
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(statusCellMargin).constraint
                })
                // 隐藏配图
                pictureView.hidden = true
            }
        }
    }
      
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(nemberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avaterImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        // 01
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(statusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(statusCellMargin)
        }
        // 03
        nemberImageView.snp_makeConstraints { (make) -> Void in
        make.leading.equalTo(nameLabel.snp_trailing).offset(statusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        // 04
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(statusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        // 06
        avaterImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        // 07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo(headImageView)
//            make.centerX.equalTo(self) // 改了一下 有问题找这边
        }
        // 08
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo((SCREENW - (cellWH * 3 + cellMargin * 2))/2)

        }
        // 给当前原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    // 微博等级
    private lazy var nemberImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 微博时间
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    // 认证Logo
    private lazy var avaterImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 20) // 改了一下 有问题找这边
    // 配图
    private lazy var pictureView: XMStatusPictureView = XMStatusPictureView()
}

